//
//  BasicAnimationController.m
//  CAAnimation
//
//  Created by 周文成 on 2017/12/12.
//  Copyright © 2017年 C-LOVE. All rights reserved.
//

#import "BasicAnimationController.h"
#import "BasicAnimationConfigure.h"
#import "KeyPath.h"

@interface BasicAnimationController ()

@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;


@property (nonatomic, strong) BasicAnimationConfigure *configure;


@end


@implementation BasicAnimationController

+ (instancetype)basicAnimationController {
    BasicAnimationController *controller = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"BasicAnimationController"];
    return controller;
}
 


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageView.layer.borderColor = [UIColor blueColor].CGColor;
    self.imageView.layer.borderWidth = 1.f;
    
    [self performSelector:@selector(animation) withObject:nil afterDelay:.5];
    
}

- (BasicAnimationConfigure *)configure {
    if(!_configure){
        _configure = [[BasicAnimationConfigure alloc] init];
    }
    return _configure;
}


- (void)setIndex:(NSInteger)index {
    _index = index;
    
    switch (index) {
        case 0:{ /// 比例缩放 transform.scale
            self.configure.keyPath = PATH_TRANSFROM_SCALE;
            self.configure.toValue = @(2);
        }
            break;
        case 1:{ /// X方向比例缩放 transform.scale.x
            self.configure.keyPath = PATH_TRANSFROM_SCALE_X;
            self.configure.toValue = [NSNumber numberWithFloat:1.5];
        }
            break;
        case 2:{ /// Y方向比例缩放 transform.scale.y
            self.configure.keyPath = PATH_TRANSFROM_SCALE_Y;
            self.configure.toValue = @(0.5);
        }
            break;
        case 3:{ /// 旋转 transform.rotation
            self.configure.keyPath = PATH_TRANSFROM_ROTATON;
            self.configure.toValue = @(M_PI_2);
        }
            break;
        case 4:{ /// 沿X旋转 transform.rotation.x
            self.configure.keyPath = PATH_TRANSFROM_ROTATON_X;
            self.configure.toValue = @(M_PI);
        }
            break;
        case 5:{ /// 沿Y旋转 transform.rotation.y
            self.configure.keyPath = PATH_TRANSFROM_ROTATON_Y;
            self.configure.toValue = @(2 * M_PI);
        }
            break;
        case 6:{ /// 沿Z旋转 transform.rotation.z
            self.configure.keyPath = PATH_TRANSFROM_ROTATON_Z;
            self.configure.toValue = @(M_PI_2);
        }
            break;
        case 7:{ /// 移动 transform.translation
            self.configure.keyPath = PATH_TRANSFROM_TRANSLATION;
            self.configure.toValue = [NSValue valueWithCGPoint:CGPointMake(100, 100)];
        }
            break;
        case 8:{ /// 沿X移动 transform.translation.x
            self.configure.keyPath = PATH_TRANSFROM_TRANSLATION_X;
            self.configure.toValue = @(100);
        }
            break;
        case 9:{ /// 沿Y移动 transform.translation.y
            self.configure.keyPath = PATH_TRANSFROM_TRANSLATION_Y;
            self.configure.toValue = @(100);
        }
            break;
        case 10:{ /// 透明度 opacity
            self.configure.keyPath = PATH_OPACITY;
            self.configure.toValue = @(0.1);
        }
            break;
        case 11:{ /// 背景色 backgroundColor
            self.configure.keyPath = PATH_BACKGROUNDCOLOR;
            self.configure.toValue = (__bridge id)[UIColor purpleColor].CGColor;
        }
            break;
        case 12:{ /// 圆角 cornerRadius
            
            self.configure.keyPath = PATH_CORNERRADIUS;
            self.configure.toValue = @(20);
        }
            break;
        case 13:{ /// 边框宽度 borderWidth
            self.configure.keyPath = PATH_BORDERWIDTH;
            self.configure.toValue = @(5);
        }
            break;
        case 14:{ /// 大小 bounds
            self.configure.keyPath = PATH_BOUNDS;
            self.configure.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 100, 100)];
        }
            break;
        case 15:{ /// 内容 contents
            self.configure.keyPath = PATH_CONTENTS;
            self.configure.toValue = (__bridge id)[UIImage imageNamed:@"grass"].CGImage;
        }
            break;
        case 16:{ /// 位置 position
            self.configure.keyPath = PATH_POSITION;
            self.configure.toValue = [NSValue valueWithCGPoint:CGPointMake(0, 100)];
        }
            break;
            
        default:
            break;
    }
}


- (void)animation {
    CABasicAnimation *animation = [CABasicAnimation animation];
    [self.configure configureWithBasicAnimation:animation];
    [self.imageView.layer addAnimation:animation forKey:[NSString stringWithFormat:@"%@Animation", self.configure.keyPath]];
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    
    /** 隔一次执行一次 退回动画 */
    static NSInteger index = 0;
    index ++;
    self.configure.autoreverses = (index % 2 != 0);
    
    [self animation];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
